package com.menu.system.service.impl;

import com.menu.common.core.domain.TreeSelect;
import com.menu.common.core.domain.entity.MenuProtype;
import com.menu.common.utils.DateUtils;
import com.menu.common.utils.SecurityUtils;
import com.menu.system.domain.vo.ProductypeInfoVo;
import com.menu.system.mapper.MenuProtypeMapper;
import com.menu.system.service.IMenuProtypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Security;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 产品类别Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-07
 */
@Service
public class MenuProtypeServiceImpl implements IMenuProtypeService {
    @Autowired
    private MenuProtypeMapper menuProtypeMapper;

    /**
     * 查询产品类别
     *
     * @param productId 产品类别主键
     * @return 产品类别
     */
    @Override
    public MenuProtype selectMenuProtypeByProductId(Long productId) {
        return menuProtypeMapper.selectMenuProtypeByProductId(productId);
    }

    /**
     * 查询产品类别列表
     *
     * @param menuProtype 产品类别
     * @return 产品类别
     */
    @Override
    public List<MenuProtype> selectMenuProtypeList(MenuProtype menuProtype) {
        return menuProtypeMapper.selectMenuProtypeList(menuProtype);
    }

    /**
     * 新增产品类别
     *
     * @param menuProtype 产品类别
     * @return 结果
     */
    @Override
    @Transactional
    public int insertMenuProtype(MenuProtype menuProtype) {

        if(menuProtype.getParentId().equals(0L)){
            menuProtype.setAncestors("0");//非顶级节点

        }else {
            MenuProtype menuProtype1 = menuProtypeMapper.selectMenuProtypeByProductId(menuProtype.getParentId());
            menuProtype.setAncestors(menuProtype1.getAncestors() + "," + menuProtype1.getProductId());
        }
        menuProtype.setCreateBy(SecurityUtils.getUsername());
        menuProtype.setCreateTime(DateUtils.getNowDate());
        int i = menuProtypeMapper.insertMenuProtype(menuProtype);
//        if (i > 0) {
//            List<ProductypeInfoVo> userPostList=new ArrayList<>();
//            for (Long attributeId : menuProtype.getAttributeIds()) {
//                ProductypeInfoVo productypeInfoVo=new ProductypeInfoVo();
//                productypeInfoVo.setAttributeId(attributeId);
//                productypeInfoVo.setProducTypeId(menuProtype.getProductId());
//                userPostList.add(productypeInfoVo);
//            }
//            menuProtypeMapper.batchMenuProductInfoPost(userPostList);
//        }
        return i;
    }

    /**
     * 修改产品类别
     *
     * @param menuProtype 产品类别
     * @return 结果
     */
    @Override
    public int updateMenuProtype(MenuProtype menuProtype) {
        menuProtype.setUpdateTime(DateUtils.getNowDate());
        return menuProtypeMapper.updateMenuProtype(menuProtype);
    }

    /**
     * 批量删除产品类别
     *
     * @param productIds 需要删除的产品类别主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteMenuProtypeByProductIds(Long[] productIds) {

        for (Long productId : productIds) {
            if (!menuProtypeMapper.delectMenuProtypeByProductId(productId).isEmpty()) {
                return 0;
            }

        }


        return menuProtypeMapper.deleteMenuProtypeByProductIds(productIds);
    }

    /**
     * 删除产品类别信息
     *
     * @param productId 产品类别主键
     * @return 结果
     */
    @Override
    public int deleteMenuProtypeByProductId(Long productId) {
        return menuProtypeMapper.deleteMenuProtypeByProductId(productId);
    }

    @Override
    public List<TreeSelect> buildMenuTreeSelect(List<MenuProtype> menus) {
        List<MenuProtype> menuTrees = buildMenuTree(menus);
        return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<MenuProtype> selectMenuProtypeByorder(Long orderNum) {
        return menuProtypeMapper.selectMenuProtypeByorder(orderNum);
    }

    @Override
    public List<MenuProtype> selectMenuProtypeids(List<String> productNames) {
        return menuProtypeMapper.selectMenuProtypeids(productNames);
    }

    @Override
    public MenuProtype selectMenuFindProductName(MenuProtype menuProtype) {
        return menuProtypeMapper.selectMenuFindProductName(menuProtype);
    }

    @Override
    public List<MenuProtype> buildMenuTree(List<MenuProtype> menus) {
        List<MenuProtype> returnList = new ArrayList<MenuProtype>();
        List<Long> tempList = menus.stream().map(MenuProtype::getProductId).collect(Collectors.toList());
        for (Iterator<MenuProtype> iterator = menus.iterator(); iterator.hasNext(); ) {
            MenuProtype menu = (MenuProtype) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getParentId())) {
                recursionFns(menus, menu);
                returnList.add(menu);
            }
        }
        if (returnList.isEmpty()) {
            returnList = menus;
        }
        return returnList;
    }

    private void recursionFns(List<MenuProtype> list, MenuProtype t) {
        // 得到子节点列表
        List<MenuProtype> childList = getChildList(list, t);
        t.setChildren(childList);
        for (MenuProtype tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFns(list, tChild);
            }
        }
    }

    private List<MenuProtype> getChildList(List<MenuProtype> list, MenuProtype t) {
        List<MenuProtype> tlist = new ArrayList<MenuProtype>();
        Iterator<MenuProtype> it = list.iterator();
        while (it.hasNext()) {
            MenuProtype n = (MenuProtype) it.next();
            if (n.getParentId().longValue() == t.getProductId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<MenuProtype> list, MenuProtype t) {
        return getChildList(list, t).size() > 0;
    }
}
